/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.VeTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class VeTau_DAO {
    
    public static String Get_MaVT() throws SQLException{
        String MaVT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "VE");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaVT = Rs.getString(1);
        }
        return MaVT;
    }
    
    public static ArrayList<VeTau_DTO> GetALL()
    {
        ArrayList<VeTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from VeTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                VeTau_DTO VeTau = new VeTau_DTO();
                VeTau.setMaVe(rs.getString("MaVe"));
                VeTau.setTenVe(rs.getString("TenVe"));
                VeTau.setMaGhe(rs.getInt("MaGhe"));
                VeTau.setMaGiuong(rs.getInt("MaGiuong"));
                VeTau.setGiaVe(rs.getDouble("GiaVe"));
                VeTau.setTinhTrangVe(rs.getBoolean("TinhTrangVe"));
                VeTau.setSoKm(rs.getDouble("SoKm"));
                VeTau.setNgayXP(rs.getDate("NgayXP"));
                VeTau.setMaDT(rs.getString("MaDT"));
                VeTau.setMaCT(rs.getString("MaCT"));
                ds.add(VeTau);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(VeTau_DTO VeTau)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayXP = fmt.format(VeTau.getNgayXP());
            String sql = "Insert VeTau Values('"+VeTau.getMaVe()+"','"+VeTau.getTenVe()+"','"+VeTau.getMaGhe()+"','"+VeTau.getMaGiuong()+"','"+VeTau.getGiaVe()+"','"+VeTau.isTinhTrangVe()+"','"+VeTau.getSoKm()+"','"+NgayXP+"','"+VeTau.getMaDT()+"','"+VeTau.getMaCT()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(VeTau_DTO VeTau)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayXP = fmt.format(VeTau.getNgayXP());
            String sql = "Update VeTau Set TenVe = '"+ VeTau.getTenVe() +"', MaGhe = "+VeTau.getMaGhe()+", MaGiuong = "+VeTau.getMaGiuong()+", GiaVe = "+ VeTau.getGiaVe()+", TinhTrangVe = "+VeTau.isTinhTrangVe()+", SoKm = "+VeTau.getSoKm()+", NgayXP ='"+NgayXP+"',MaDT ='"+VeTau.getMaDT()+"',MaCT = '"+VeTau.getMaCT()+"' where MaVe = '"+ VeTau.getMaVe()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maVe)
    {
        boolean flag = false;
        try {
            String sql = "Delete from VeTau where MaVe = '"+maVe+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
